package project.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import schronisko.Queries;

/**
 * Servlet implementation class Message
 */
@WebServlet("/Message")
public class Message extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	Queries q = new Queries();
	HttpSession s;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Message() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		s = request.getSession();
		String type = request.getParameter("type");
		
		if(type.equals("del")) {	
			int ID = Integer.parseInt(request.getParameter("id"));
			if(q.deleteMessage(ID) == 0){
				s.setAttribute("deleted_msg", 1);
				response.sendRedirect("admin/new_msg.jsp");
			}
		}
		else if(type.equals("mark")){
			int ID = Integer.parseInt(request.getParameter("id"));
			q.viewMessage(ID,1);
			s.setAttribute("mark_msg", 1);
			response.sendRedirect("admin/new_msg.jsp");
		}
		else if(type.equals("mark0")){
			int ID = Integer.parseInt(request.getParameter("id"));
			q.viewMessage(ID,0);
			s.setAttribute("mark_msg0", 1);
			response.sendRedirect("admin/old_msg.jsp");
		}
		else if (type.equals("ans")){
			String email = request.getParameter("email");
			int ID = Integer.parseInt(request.getParameter("id"));
			s.setAttribute("id_msg", ID);
			s.setAttribute("email", email);
			response.sendRedirect("admin/answer.jsp");
		}
	}

}
